{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56db8590-2121-4e46-bf13-7044132e4e7a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "630351a2-f9ec-42da-8908-e82ecfbbdc61",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_207991/3527668302.py:10: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead.\n",
      "  \"visit_date\": pd.date_range('2025-06-01', periods=2000, freq='H'),\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from datetime import datetime\n",
    "\n",
    "# ===== 1. 模拟数据集（2000条用户行为记录） =====\n",
    "np.random.seed(42)\n",
    "data = {\n",
    "    \"user_id\": np.arange(1000, 3000),\n",
    "    \"visit_date\": pd.date_range('2025-06-01', periods=2000, freq='H'),\n",
    "    \"visit\": np.random.choice([0, 1], size=2000, p=[0.1, 0.9]),\n",
    "    \"click\": np.random.choice([0, 1], size=2000, p=[0.4, 0.6]),\n",
    "    \"add_to_cart\": np.random.choice([0, 1], size=2000, p=[0.7, 0.3]),\n",
    "    \"payment\": np.where(np.random.rand(2000) > 0.85, 1, 0),  # 15%支付转化率\n",
    "    \"order_amount\": np.round(np.abs(np.random.normal(100, 40, 2000)), 2)\n",
    "}\n",
    "df = pd.DataFrame(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f208bef1-98bb-4da6-a373-293835a3f647",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>visit_date</th>\n",
       "      <th>visit</th>\n",
       "      <th>click</th>\n",
       "      <th>add_to_cart</th>\n",
       "      <th>payment</th>\n",
       "      <th>order_amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1000</td>\n",
       "      <td>2025-06-01 00:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>138.35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1001</td>\n",
       "      <td>2025-06-01 01:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>145.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1002</td>\n",
       "      <td>2025-06-01 02:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>22.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1003</td>\n",
       "      <td>2025-06-01 03:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>139.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1004</td>\n",
       "      <td>2025-06-01 04:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>67.58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995</th>\n",
       "      <td>2995</td>\n",
       "      <td>2025-08-23 03:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>115.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996</th>\n",
       "      <td>2996</td>\n",
       "      <td>2025-08-23 04:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>92.51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997</th>\n",
       "      <td>2997</td>\n",
       "      <td>2025-08-23 05:00:00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>141.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998</th>\n",
       "      <td>2998</td>\n",
       "      <td>2025-08-23 06:00:00</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>158.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999</th>\n",
       "      <td>2999</td>\n",
       "      <td>2025-08-23 07:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>80.80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2000 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      user_id          visit_date  visit  click  add_to_cart  payment  \\\n",
       "0        1000 2025-06-01 00:00:00      1      0            0        0   \n",
       "1        1001 2025-06-01 01:00:00      1      0            1        0   \n",
       "2        1002 2025-06-01 02:00:00      1      1            1        1   \n",
       "3        1003 2025-06-01 03:00:00      1      0            0        0   \n",
       "4        1004 2025-06-01 04:00:00      1      0            0        0   \n",
       "...       ...                 ...    ...    ...          ...      ...   \n",
       "1995     2995 2025-08-23 03:00:00      1      1            0        0   \n",
       "1996     2996 2025-08-23 04:00:00      1      0            0        0   \n",
       "1997     2997 2025-08-23 05:00:00      0      0            1        0   \n",
       "1998     2998 2025-08-23 06:00:00      0      1            0        1   \n",
       "1999     2999 2025-08-23 07:00:00      1      0            0        0   \n",
       "\n",
       "      order_amount  \n",
       "0           138.35  \n",
       "1           145.11  \n",
       "2            22.11  \n",
       "3           139.14  \n",
       "4            67.58  \n",
       "...            ...  \n",
       "1995        115.09  \n",
       "1996         92.51  \n",
       "1997        141.31  \n",
       "1998        158.15  \n",
       "1999         80.80  \n",
       "\n",
       "[2000 rows x 7 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e32f7416-42a4-499e-9ee1-6f9d45d5d8df",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_207991/2253426399.py:6: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df['order_amount'] = np.where(\n",
      "/tmp/ipykernel_207991/2253426399.py:13: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df['visit_hour'] = df['visit_date'].dt.hour\n",
      "/tmp/ipykernel_207991/2253426399.py:14: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df['visit_day'] = df['visit_date'].dt.day_name()\n"
     ]
    }
   ],
   "source": [
    "# ===== 2. 数据清洗与预处理 =====\n",
    "# 删除从未访问的用户（Pandas布尔索引）\n",
    "df = df[df['visit'] == 1]  \n",
    "\n",
    "# 修正异常订单金额（Numpy条件逻辑）\n",
    "df['order_amount'] = np.where(\n",
    "    df['order_amount'] > 500, \n",
    "    500, \n",
    "    np.where(df['order_amount'] < 10, 10, df['order_amount'])\n",
    ")\n",
    "\n",
    "# 提取关键时间特征（Pandas日期处理）\n",
    "df['visit_hour'] = df['visit_date'].dt.hour\n",
    "df['visit_day'] = df['visit_date'].dt.day_name()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8695b77-342f-4f53-8a6f-2d5f1f84999b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fef8f02-c4da-489b-95bf-f4c8b63c02fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== 3. 漏斗转化分析（中等复杂度核心） =====\n",
    "funnel_steps = ['visit', 'click', 'add_to_cart', 'payment']\n",
    "funnel_data = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5211c58-54f4-421f-b254-9e60ec2b010e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
